package com.ccn.crm.interfaces.impl;

import com.ccn.crm.model.MailBean;
import com.ccn.crm.model.User;
import com.ccn.crm.model.equipment;
import com.ccn.crm.model.salePda;
import com.ccn.crm.service.IEquipment;
import com.ccn.crm.service.ISalePda;
import com.ccn.crm.service.IUser;
import com.ccn.crm.util.DateUtil;
import com.ccn.crm.util.EmailUtil;
import com.ccn.crm.util.GetToken;
import com.ccn.crm.util.GetUserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
@Service
public class EquipmentEmail implements IEquipmentEmail{
    @Autowired
    private IEquipment iEquipment;
    @Autowired
    private ISalePda iSalePda;
    @Autowired
    private EmailUtil emailUtil;
    @Autowired
    private IUser iUser;
    @Override
    public void sentEquipmentEmail() {
        List<equipment> equipm = iEquipment.selectAll();

        for (equipment equipment :equipm){
            Date refuseddate = equipment.getReturndate();
            //预计归还时间大于现在时间
//            if (refuseddate.getTime()>= DateUtil.getTodayTimeCuo()){
                Date returndate = equipment.getReturndate();
                if (refuseddate==null){
                    continue;
                }

                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
                String reTime = format.format(returndate);
                String createdby = equipment.getCreatedby();
                User user = iUser.selectByAccount(createdby);
                if (null==user){
                    continue;
                }
                MailBean mailBean = new MailBean();
//                if (returndate.getTime()>= (DateUtil.getTodayTimeCuo()-60000*60*24)&&returndate.getTime()<= (DateUtil.getTodayTimeCuo()+60000*60*24*2)){
                if ((returndate.getTime()-60000*60*24*2)>=DateUtil.getTodayTimeCuo()&&(returndate.getTime()-60000*60*24*3)<=DateUtil.getTodayTimeCuo()){
                    String sent=user.getEmail();
                    String content=equipment.getPdamodel();
                    String text="<html>\n" +
                            "<body>\n" +
                            "<h3>上海中商网络股份有限公司（项目组）</h3>\n" +
                            "<h5>" + "你借出的设备--"+content+"快到预计归还日期"+"("+reTime+")"+"如果需要请归还后重新申请使用"+"</h5>\n"+
                            "<body>\n" +
                            "</html>\n";
                    mailBean.setSubject("设备归还提醒");
                    mailBean.setText(text);
                    emailUtil.sendMailHtmlByOne(mailBean,sent);
                }
                int i = DateUtil.dayForWeekByDate(refuseddate);
                if ((DateUtil.dayForWeek()==i&&returndate.getTime()<DateUtil.getTodayTimeCuo())){
                    String sent=user.getEmail();
                    String content=equipment.getPdamodel();
                    String text="<html>\n" +
                            "<body>\n" +
                            "<h3>上海中商网络股份有限公司（项目组）</h3>\n" +
                            "<h5>" + "你借出的设备--"+content+"已经预期"+"(借出时间："+reTime+")"+"，如果需要请归还后重新申请使用，谢谢。"+"</h5>\n"+
                            "<body>\n" +
                            "</html>\n";
                    mailBean.setSubject("设备归还提醒");
                    mailBean.setText(text);
                    emailUtil.sendMailHtmlByOne(mailBean,sent);

                }
//            }
        }

        List<salePda> salePdas = iSalePda.selectAll();
        for (salePda salePd :salePdas){
            Date returndate = salePd.getReturndate();
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
            String reTime = format.format(returndate);
            String createdby = salePd.getCreatedby();
            if ((returndate.getTime()-60000*60*24*2)>=DateUtil.getTodayTimeCuo()&&(returndate.getTime()-60000*60*24*3)<=DateUtil.getTodayTimeCuo()){

                ArrayList<Object> objects = new ArrayList<>();
                objects.add(createdby);
                Map<String, Object> token = GetToken.getToken();
                String corpAccessToken = (String) token.get("corpAccessToken");
                String id = GetUserInfo.GetUserId(objects, token);
                if (null==id){
                    continue;
                }
                ArrayList<Object> objectsId = new ArrayList<>();
                objectsId.add(id);
                Map<String, Object> stringObjectMap = GetUserInfo.GetUserInfo(objectsId, corpAccessToken);
                if (stringObjectMap.get("errorMessage").equals("success")) {
                    if (null != stringObjectMap.get("email")) {
                        String email = stringObjectMap.get("email").toString();
                        MailBean mailBean = new MailBean();
                        String content =  salePd.getPdamodel();
                        String text = "<html>\n" +
                                "<body>\n" +
                                "<h3>上海中商网络股份有限公司（项目组）</h3>\n" +
                                "<h5>" + "你借出的设备--"+content+"快到预计归还日期"+"("+reTime+")"+"如果需要请归还后重新申请使用"+"</h5>\n"+
                                "<body>\n" +
                                "</html>\n";
                        mailBean.setSubject("设备归还提醒");
                        mailBean.setText(text);
                        emailUtil.sendMailHtmlByOne(mailBean,email);

                    }
                }

            }
            int i = DateUtil.dayForWeekByDate(returndate);
            if ((DateUtil.dayForWeek()==i&&returndate.getTime()<DateUtil.getTodayTimeCuo())){

                ArrayList<Object> objects = new ArrayList<>();
                objects.add(createdby);
                Map<String, Object> token = GetToken.getToken();
                String corpAccessToken = (String) token.get("corpAccessToken");
                String id = GetUserInfo.GetUserId(objects, token);
                ArrayList<Object> objectsId = new ArrayList<>();
                objectsId.add(id);
                Map<String, Object> stringObjectMap = GetUserInfo.GetUserInfo(objectsId, corpAccessToken);
                if (stringObjectMap.get("errorMessage").equals("success")) {
                    if (null != stringObjectMap.get("email")) {
                        String email = stringObjectMap.get("email").toString();

                        MailBean mailBean = new MailBean();

                        String content =  salePd.getPdamodel();
                        String text = "<html>\n" +
                                "<body>\n" +
                                "<h3>上海中商网络股份有限公司（项目组）</h3>\n" +
                                "<h5>" + "你借出的设备--"+content+"已经预期"+"(借出时间："+reTime+")"+"，如果需要请归还后重新申请使用，谢谢。"+"</h5>\n"+
                                "<body>\n" +
                                "</html>\n";
                        mailBean.setSubject("设备归还提醒");
                        mailBean.setText(text);
                        emailUtil.sendMailHtmlByOne(mailBean,email);

                    }
                }
            }
        }
    }
}
